/*
 *  Copyright (C) 2010 MADSA.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author:  Jorge Pintado de Santiago
 *          
 */

package com.madsa.maps;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.location.Location;

public class MyPositionOverlay extends Overlay {
	private final int mRadius = 5;
	Location location;
	public Location getLocation() {
	  return location;
	}
	public void setLocation(Location location) {
	  this.location = location;
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
	  Projection projection = mapView.getProjection();
	  if (shadow == false) {
	    // Get the current location
	    Double latitude = location.getLatitude()*1E6;
	    Double longitude = location.getLongitude()*1E6;
	    GeoPoint geoPoint;
	    geoPoint = new GeoPoint(latitude.intValue(),longitude.intValue());
	    // Convert the location to screen pixels
	    Point point = new Point();
	    projection.toPixels(geoPoint, point);
	    RectF oval = new RectF(point.x - mRadius, point.y - mRadius,
	                           point.x + mRadius, point.y + mRadius);
	    // Setup the paint
	    Paint paint = new Paint();
	    paint.setARGB(250, 255, 0, 0);
	    paint.setAntiAlias(true);
	    paint.setFakeBoldText(true);
	    Paint backPaint = new Paint();
	    backPaint.setARGB(175, 50, 50, 50);
	    backPaint.setAntiAlias(true);
	    RectF backRect = new RectF(point.x + 2 + mRadius,
	                               point.y - 3*mRadius,
	                               point.x + 80, point.y + mRadius);
	    // Draw the marker
	    canvas.drawOval(oval, paint);
	    canvas.drawRoundRect(backRect, 5, 5, backPaint);
	    canvas.drawText("Here You Are", point.x + 2*mRadius, point.y, paint);
	  }
	  super.draw(canvas, mapView, shadow);
	}

  @Override
  public boolean onTap(GeoPoint point, MapView mapView) {
    return false;
  }
}